﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Lead2012.Models;
using Lead2012.ViewModels;
using Lead2012.Helpers;
using Lead2012.Business;
using System.Data;
using System.Data.SqlClient;
using Lead2012.Utility;

namespace Lead2012.Repository
{
    public class LeadStatisticManagementRepository : BaseManagementRepository
    {
        // Create an instance of entities framework
        Lead2012Entities entities = new Lead2012Entities();

        public void GetStatisticModel(ref LeadStatisticViewModel model)
        {
            model.ChartModel = GetChartModel(model);
        }

        #region Private method
        private ChartViewModel GetChartModel(LeadStatisticViewModel model)
        {
            ChartViewModel chartModel = new ChartViewModel();

            chartModel.Ticks = GetAllTicks();
            SetDataModel(model, ref chartModel);

            return chartModel;
        }

        private string GetAllTicks()
        {
            return "['Tháng 1', 'Tháng 2', 'Tháng 3', 'Tháng 4', 'Tháng 5', 'Tháng 6', 'Tháng 7', 'Tháng 8', 'Tháng 9', 'Tháng 10', 'Tháng 11', 'Tháng 12']";
        }
        
        private void SetDataModel(LeadStatisticViewModel model, ref ChartViewModel chartModel)
        {
            string data = string.Empty;
            string serires = string.Empty;

            // With year
            if (model.FilterType == "year")
            {
                if (model.ChartType == "Bar")
                {
                    var list = GetAllStatistic(model.StatisticType, int.Parse(model.FilterOption));
                    string sFormat = @"{0}[{1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}]";

                    // Calculate oject
                    for (int i = 0; i < list.Count; i++)
                    {
                        StatisticFilter sf = list[i];
                        if (i == 0)
                        {
                            data += string.Format(sFormat, string.Empty, sf.T1, sf.T2, sf.T3, sf.T4, sf.T5, sf.T6, sf.T7, sf.T8, sf.T9, sf.T10, sf.T11, sf.T12);
                            serires += "{label:'" + list[i].ItemName + "'}";
                        }
                        else
                        {
                            data += string.Format(", " + sFormat, string.Empty, sf.T1, sf.T2, sf.T3, sf.T4, sf.T5, sf.T6, sf.T7, sf.T8, sf.T9, sf.T10, sf.T11, sf.T12);
                            serires += ",{label:'" + list[i].ItemName + "'}";
                        }
                    }
                }
                else if (model.ChartType == "Line")
                {
                    // Calculate oject
                    var list = GetAllStatistic(model.StatisticType, int.Parse(model.FilterOption));
                    for (int i = 0; i < list.Count; i++)
                    {
                        StatisticFilter sf = list[i];
                        if (i > 0)
                            data += ",";
                        data += "[";
                        data += string.Format("[{0}, {1}]", 1, sf.T1);
                        data += string.Format(",[{0}, {1}]", 2, sf.T2);
                        data += string.Format(",[{0}, {1}]", 3, sf.T3);
                        data += string.Format(",[{0}, {1}]", 4, sf.T4);
                        data += string.Format(",[{0}, {1}]", 5, sf.T5);
                        data += string.Format(",[{0}, {1}]", 6, sf.T6);
                        data += string.Format(",[{0}, {1}]", 7, sf.T7);
                        data += string.Format(",[{0}, {1}]", 8, sf.T8);
                        data += string.Format(",[{0}, {1}]", 9, sf.T9);
                        data += string.Format(",[{0}, {1}]", 10, sf.T10);
                        data += string.Format(",[{0}, {1}]", 11, sf.T11);
                        data += string.Format(",[{0}, {1}]", 12, sf.T12);
                        data += "]";
                    }
                }
                else // Pie
                {
                    var list = GetAllStatisticByPie(model.StatisticType, int.Parse(model.FilterOption));
                    data += "[";
                    for (int i = 0; i < list.Count; i++)
                    {
                        if (i > 0)
                            data += ",";
                        data += string.Format("['{0}', {1}]", list[i].ItemName, list[i].ItemCount);
                    }
                    data += "]";
                }

                chartModel.PlotData = data;
                chartModel.Series = serires;
            }
        }

        private List<StatisticFilter> GetAllStatistic(string statisticType, int year)
        {
            List<StatisticFilter> list = new List<StatisticFilter>();

            // Get list item on each month
            // Params
            SqlParameter paramType = new SqlParameter("@StatisticType", DbType.String);
            paramType.Value = statisticType;

            SqlParameter paramYear = new SqlParameter("@Year", DbType.Int32);
            paramYear.Value = year;

            using (SqlDataReader item = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringProfile, CommandType.StoredProcedure, "GetStatisticByType", paramType, paramYear))
            {
                while (item.Read())
                {
                    list.Add(new StatisticFilter()
                    {
                        ItemName = item.GetString(0),
                        T1 = item.GetInt32(1),
                        T2 = item.GetInt32(2),
                        T3 = item.GetInt32(3),
                        T4 = item.GetInt32(4),
                        T5 = item.GetInt32(5),
                        T6 = item.GetInt32(6),
                        T7 = item.GetInt32(7),
                        T8 = item.GetInt32(8),
                        T9 = item.GetInt32(9),
                        T10 = item.GetInt32(10),
                        T11 = item.GetInt32(11),
                        T12 = item.GetInt32(12),
                    });
                }
            }

            return list;
        }

        private List<StatisticFilter> GetAllStatisticByPie(string statisticType, int year)
        {
            List<StatisticFilter> list = new List<StatisticFilter>();

            // Params
            SqlParameter paramType = new SqlParameter("@StatisticType", DbType.String);
            paramType.Value = statisticType;

            SqlParameter paramYear = new SqlParameter("@Year", DbType.Int32);
            paramYear.Value = year;

            using (SqlDataReader item = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringProfile, CommandType.StoredProcedure, "GetStatisticByYear", paramType, paramYear))
            {
                while (item.Read())
                {
                    list.Add(new StatisticFilter()
                    {
                        ItemName = item.GetString(0),
                        ItemCount = item.GetInt32(1)
                    });
                }
            }

            return list;
        }
        #endregion
    }
}